from zope.interface import implements, Interface

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName
from zope.component import getUtility
from zope.component import getMultiAdapter
from Acquisition import aq_inner
from plone.memoize.instance import memoize
from my315ok.fangcan.interfaces import Ihouse

from zope.i18n.interfaces import ITranslationDomain
from zope.component import queryUtility

from Products.CMFCore.interfaces import ISiteRoot
from Products.AdvancedQuery import Eq, Between, Le
from my315ok.xtjxwcontents.browser.jiaxiao_folderview import jiaxiao_folderView
from my315ok.xtjxwcontents.interfaces.jiaolian import Ijiaolian

from my315ok.xtjxwcontents import xtjxwcontentsMessageFactory as _


class Ijianlian_searchView(Interface):
    """
    jianlian_search view interface
    """

    def test():
        """ test method"""


class jianlian_searchView(jiaxiao_folderView):
    """
    jianlian_search browser view
    """
    implements(Ijianlian_searchView)

    def query(self):
        """
        compose query string
        
        """
        portal = getUtility(ISiteRoot)
        catalog = getToolByName(portal, 'portal_catalog')
        aq = catalog.makeAdvancedQuery({'review_state': 'published','object_provides':Ijiaolian.__identifier__})
        if "SearchableText" in self.request.form.keys():
            term = self.request.form["SearchableText"]
            term = term.strip()
            if len(term)!=0:
                query = Eq("SearchableText",term) | Eq('Description',term)
                return query
                
        raw = self.request["QUERY_STRING"]
        if len(raw) != 0:
            phase = raw.split("&")
#            para = []

            for st2 in phase:                
                st = st2.split("=")
                pa = st[0]
                val = st[-1]
            
                if "-" in val:
#                op = Between
                    low = val.split("-")[0]
                    hight = val.split("-")[-1]
                    query = Between(pa,float(low),float(hight))
#                    paraitem ="""%s = { "query": [%s, %s],"range": "minmax" }""" % (pa,low,hight)
#                    para.append(paraitem)

                
                else:
#                op = Eq
                    query = Eq(pa, val)
#                    para[pa] = val
#                aq = query
                aq &= query 
            return aq

    def getImage(self,brain,scale="tile"):
        
        if scale=="orig":
            ext = "/image"
        else:
            ext = "/image_%s" % scale
#        base = brain.getURL()        
        base = self.getURL(brain)
        title = brain.Title
        url = base +  ext
#        portal = self.portal
#        import pdb
#        pdb.set_trace()
#        p_url = portal.absolute_url()
#        existimg = portal.unrestrictedTraverse(url, default=None)
#        if existimg == None:
#            url = p_url + "/++resource++schoolwatermark.png" 
#            url = base + '/main.png' + ext                           
        htmltag = "<img src='%s' alt='%s' />" %(url,title)
        return htmltag